package sync

import (
	"context"
	"encoding/json"
	erp "ez/apps/erp/document"
	"ez/apps/erp/service"
	"fmt"
	"gitee.com/dreamwood/ez-go/ez"
	"time"
)

type FLowingMessage struct {
	TargetId      int64
	IsConfirm     bool      `json:"isConfirm" bson:"isConfirm"`         //是否生效
	ConfirmAt     time.Time `json:"confirmAt" bson:"confirmAt"`         //生效时间
	IsCancel      bool      `json:"isCancel" bson:"isCancel"`           //是否取消
	IsLock        bool      `json:"isLock" bson:"isLock"`               //是否锁定
	ApprovalSate  string    `json:"approvalSate" bson:"approvalSate"`   //评审状态
	ApprovalStage string    `json:"approvalStage" bson:"approvalStage"` //评审环节
}

func (this *FLowingMessage) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *FLowingMessage) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}

func init() {
	ez.Subscribe(ez.EventAfterServerRun, func(v interface{}, ctx context.Context) {
		ez.CreateNsqHandler(createFlowingPublishTopic("erp.BuyOrder"), "default", func(content []byte) error {
			message := new(FLowingMessage)
			message.UnSerialize(content)
			//数据写入到BuyOrder
			crudBuyOrder := erp.NewBuyOrderCrud()
			buyOrder, e := crudBuyOrder.FindId(message.TargetId)
			if e != nil {
				ez.PrintError(e)
				return nil
			}
			buyOrder.IsConfirm = message.IsConfirm
			buyOrder.ConfirmAt = message.ConfirmAt
			buyOrder.IsCancel = message.IsCancel
			buyOrder.IsLock = message.IsLock
			buyOrder.ApprovalSate = message.ApprovalSate
			buyOrder.ApprovalStage = message.ApprovalStage
			e = buyOrder.Save()
			ez.Get(e)
			service.BuyOrderFillNumAndMoney(message.TargetId)
			return nil
		})
		ez.CreateNsqHandler(createFlowingPublishTopic("erp.Need"), "default", func(content []byte) error {
			message := new(FLowingMessage)
			message.UnSerialize(content)
			//数据写入到BuyOrder
			crud := erp.NewNeedCrud()
			need, e := crud.FindId(message.TargetId)
			if e != nil {
				ez.PrintError(e)
				return nil
			}
			need.IsConfirm = message.IsConfirm
			need.ConfirmAt = message.ConfirmAt
			need.IsCancel = message.IsCancel
			need.IsLock = message.IsLock
			need.ApprovalSate = message.ApprovalSate
			need.ApprovalStage = message.ApprovalStage
			e = need.Save()
			ez.Get(e)
			return nil
		})
		ez.CreateNsqHandler(createFlowingPublishTopic("erp.SellFeedback"), "default", func(content []byte) error {
			message := new(FLowingMessage)
			message.UnSerialize(content)
			//数据写入到BuyOrder
			crud := erp.NewSellFeedbackCrud()
			need, e := crud.FindId(message.TargetId)
			if e != nil {
				ez.PrintError(e)
				return nil
			}
			need.IsConfirm = message.IsConfirm
			need.ConfirmAt = message.ConfirmAt
			need.IsCancel = message.IsCancel
			//need.IsLock = message.IsLock
			//need.ApprovalSate = message.ApprovalSate
			//need.ApprovalStage = message.ApprovalStage
			e = need.Save()
			ez.Get(e)
			return nil
		})
	})
}

func createFlowingPublishTopic(targetType string) string {
	return fmt.Sprintf("ez.Flowing.%s", targetType)
}
